<template>
  <div class="product-detail">
    <s-header :name="'商品详情'"></s-header>

    <div class="detail-content">
      <div class="detail-swipe-wrap">
        <van-swipe class="my-swipe" indicator-color="#1baeae" :autoplay="2000" lazy-render>
          <van-swipe-item v-for="(item, index) in good.pictures" :key="index">
            <img :src="item" alt="">
          </van-swipe-item>
        </van-swipe>
      </div>

      <div class="product-info">
        <div class="product-title">
          {{ good.name || '' }}
        </div>
        <div class="product-desc">
          <div> {{ good.introduction }}</div>
          <div> {{ good.detail }}</div>
        </div>
        <div class="product-price">
          <span>¥{{ good.sellingPrice }} <del v-if="good.isSeckill===1">¥{{ good.markedPrice }}</del> </span>
          <span>库存{{ good.stock }}</span>
        </div>
      </div>

      <div v-if="good.isSeckill===1">
        活动详情
        <div>活动时间：{{ timestampToTime(activity.startTime) }} - {{ timestampToTime(activity.startTime) }}</div>
        <div>
          <van-count-down :time="activity.endTime - currentTime" format="结束倒计时： DD 天 HH 时 mm 分 ss 秒"/>
        </div>
      </div>

      <div v-for="(item, index) in good.pictures" :key="index">
        <van-image lazy-load :src="item"/>
      </div>

    </div>


    <van-action-bar>
      <van-action-bar-icon icon="chat-o" text="客服" @click="tips"/>
      <van-action-bar-button type="danger" @click="goBuy" text="立即购买"/>
    </van-action-bar>
  </div>
</template>

<script>
import {reactive, onMounted, toRefs} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import {Toast} from "vant";
import sHeader from '@/components/SimpleHeader'
import {getDetail} from '@/service/good'
import {getAcivityDetail} from "@/service/activity";


export default {
  setup() {
    const route = useRoute()
    const router = useRouter()

    const state = reactive({
      goodId: route.query.goodId || '',
      good: '',
      seckillStr: '',
      activity: '',
      currentTime: 0,
    })

    onMounted(async () => {
      const {good, seckillStr} = await getDetail({goodId: state.goodId})
      state.good = good
      state.good.pictures = state.good.pictures.split(',')
      state.seckillStr = seckillStr
      state.currentTime = new Date().getTime()

      if (good.isSeckill === 1) {
        const {data} = await getAcivityDetail({goodId: state.goodId})
        state.activity = data
      }
    })

    const timestampToTime = (timestamp) => {
      timestamp = timestamp ? timestamp : null;
      let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + '-';
      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
      let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
      let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
      let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s;
    }

    const goBuy = () => {
      router.push({path: '/create-order', query: {goodId: state.goodId}})
    }
    const goBack = () => {
      router.go(-1)
    }
    const tips = () => {
      Toast('暂不可用');
    }

    return {
      ...toRefs(state),
      goBack,
      tips,
      goBuy,
      timestampToTime
    }
  },
  components: {
    sHeader
  }
}
</script>


<style lang="less">
@import '../../common/style/mixin';

.product-detail {
  .detail-header {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 10000;
    .fj();
    .wh(100%, 44px);
    line-height: 44px;
    padding: 0 10px;
    .boxSizing();
    color: #252525;
    background: #fff;
    border-bottom: 1px solid #dcdcdc;

    .product-name {
      font-size: 14px;
    }
  }

  .detail-content {
    height: calc(100vh - 50px);
    overflow: hidden;
    overflow-y: auto;

    .detail-swipe-wrap {
      .my-swipe .van-swipe-item {
        img {
          width: 100%;
          // height: 300px;
        }
      }
    }

    .product-info {
      padding: 0 10px;

      .product-title {
        font-size: 18px;
        text-align: left;
        color: #333;
      }

      .product-desc {
        font-size: 14px;
        text-align: left;
        color: #999;
        padding: 5px 0;
      }

      .product-price {
        .fj();

        span:nth-child(1) {
          color: #F63515;
          font-size: 22px;
        }

        span:nth-child(2) {
          color: #999;
          font-size: 16px;
        }
      }
    }

    .product-intro {
      width: 100%;
      padding-bottom: 50px;

      ul {
        .fj();
        width: 100%;
        margin: 10px 0;

        li {
          flex: 1;
          padding: 5px 0;
          text-align: center;
          font-size: 15px;
          border-right: 1px solid #999;
          box-sizing: border-box;

          &:last-child {
            border-right: none;
          }
        }
      }

      .product-content {
        padding: 0 20px;

        img {
          width: 100%;
        }
      }
    }
  }

  .van-action-bar-button--warning {
    background: linear-gradient(to right, #6bd8d8, @primary)
  }

  .van-action-bar-button--danger {
    background: linear-gradient(to right, #0dc3c3, #098888)
  }
}
</style>
